// index.js

var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    imgUrls: app.appimg,
    index: 0,
    indicatorDots: true,
    autoplay: true,
    interval: 5000,
    duration: 1000,
    isAgree: true,
    date: app.startdate,
    linename: '',
    linenameIndex: 0,
    isSelect: false,
    id: '',
    show: false,
    text: '',
    windowwidth: '',
    scrolllength: 0,
    appColor: app.appColor

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    var that = this;
    this.data.windowwidth = wx.getSystemInfoSync().windowWidth;
  },

  codeBind: function() {
    wx.navigateTo({
      url: '../../../pages/bindPhone/bindPhone',
    });
  },
  formSubmit: function(e) {
    if (this.data.linenameIndex !== '' && this.data.show == true) {
      wx.navigateTo({
        url: '../linesdetail/linesdetail?id=' + this.data.iid
      });
    } else {
      this.Alert();
    };

  },
  hideModal: function() {
    this.setData({
      modalBox: false
    })
  },
  // 扫码
  scanCode: function() {
    wx.scanCode({
      success: (res) => {
        console.log(res)
        wx.navigateTo({
          url: '../linepre/linepre?scancode=' + res.result,
        })

      }
    })
    // if (app.IsRegister) {
    //   wx.scanCode({
    //     success: (res) => {
    //       wx.navigateTo({
    //         url: '../linepre/linepre?scancode=' + res.result,
    //       })

    //     }
    //   })
    // } 
    // else {
      // this.setData({
      //   modalBox: true
      // })
    // }

  },
  selectDate: function() {
    wx.navigateTo({
      url: '../../../pages/calendar/calendar'
    });
  },

  // 手机绑定模态框
  // 微信授权绑定
  getPhoneNumber: function(e) {
    var that = this;
    if (e.encryptedData != '' && e.iv != '') {
      wx.request({
        url: app.serviceurl + 'Member/WxHandsetBinding',
        header: {
          Authorization: "Bearer " + app.accesstoken
        },
        data: {
          EncryptedData: e.detail.encryptedData,
          IV: e.detail.iv,
          Code: app.codes
        },
        dataType: 'json',
        success: function(res) {
          if (!res.data.Result) {
            //  提示框
            wx.showModal({
              content: res.data.Message,
              showCancel: false,
              success: function(res) {}
            });
            return
          }
          app.IsRegister = true;
          app.HandsetNumber = res.data.HandsetNumber;
          wx.showModal({
            content: '绑定成功',
            showCancel: false,
            success: function(res) {}
          });
        },
        fail: function() {}
      })
    } else {
      wx.showModal({
        content: '抱歉，您的微信暂未绑定手机号，请使用短信验证码绑定',
        showCancel: false,
        success: function(res) {
          if (res.confirm) {
            wx.navigateTo({
              url: '../../../pages/bindPhone/bindPhone',
            });
          }
        }
      });
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    this.getActivity();

    this.setData({
      text: app.notice,
      date: app.startdate
    });
    if (app.customline.linename != "") {
      this.setData({
        isSelect: true
      });
    }
    var that = this;
    try {
      wx.getStorage({
        key: "customlinename",
        success: function(res) {
          that.setData({
            linename: res.data
          });
          if (res.data.length > 0) {
            that.setData({
              isSelect: true,
              show: true
            });
          }
        }
      })
    } catch (err) {}

    var that = this;
    wx.request({
      url: app.serviceurl + 'CustomLine/CustomQueryLine',
      header: {
        Authorization: "Bearer " + app.accesstoken
      },
      data: {},
      dataType: 'json',
      success: function(res) {
        if (!res.data.Result) {
          that.setData({
            show: false
          })
          return
        };
        var path = [],
          id = [],
          reserve = res.data.CustomLines,
          i = 0,
          indexs = 0;
        if (res.data.Result) {
          reserve.forEach(function(e) {
            path.push(e.busLineName)
            id.push(e.busLineID);
            if (e.busLineName == that.data.linename) {
              indexs = i;
            }
            i++;
          })
        } else {
          that.setData({
            show: false
          })
        }

        that.setData({
          data: res.data.CustomLines,
          path: path,
          id: id,
          ref: res.data
        })
        that.setData({
          linenameIndex: indexs,
          iid: that.data.id[indexs]
        });

      },
      fail: function(err) {},
      complete: function() {

      }
    });
  },

  getActivity: function() {
    var that = this;
    wx.request({
      url: app.serviceurl + '/CustomLine/CustomQueryDiscount',
      header: {
        Authorization: "Bearer " + app.accesstoken
      },
      dataType: 'json',
      success: function(res) {
        that.setData({
          acresult: res.data.Result
        })

      },
      fail: function() {}
    })
  },
  none: function() {
    wx.showModal({
      content: '很抱歉，暂时没有路线',
      showCancel: false,
      success: function(res) {}
    });
  },
  evade: function() {
    if (!this.data.path) {
      this.none();
    }
  },
  chooseline: function(e) {
    try {
      this.setData({
        linenameIndex: e.detail.value,
        show: true
      });
      wx.setStorage({
        key: "customlinename",
        data: this.data.path[this.data.linenameIndex]
      });
      this.data.iid = this.data.id[e.detail.value];
    } catch (err) {
      this.setData({
        show: false
      });
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function(res) {
    if (res.from === 'menu') {
      // 来自页面内转发按钮
      return app.OnShare();
    }
  },

  bindDateChange: function(e) {
    app.startdate = e.detail.value;
    this.setData({
      date: e.detail.value
    })
  },


  // 提示框
  Alert: function() {
    wx.showModal({
      content: '请选择您的出行线路，以便我们为您服务！',
      showCancel: false,
      success: function(res) {

      }
    });
  },

  bindAgreeChange: function(e) {
    this.setData({
      isAgree: !e.currentTarget.dataset.isagree
    });
  },

  callservice: function(e) {
    wx.makePhoneCall({
      phoneNumber: "96606"
    });
  },
  gorules: function() {
    wx: wx.navigateTo({
      url: '../activerules/activerules',
    })
  }
})